<template>
  <div class="app-container">
    <el-form :model="queryParams2" ref="queryForm2" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="表名" prop="tableName">
        <el-input disabled="true" style="width: 300px"
                  v-model="queryParams2.tableName"
                  placeholder="请输入表名"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文名称" prop="tableChineseName">
        <el-input disabled="true"  style="width: 300px"
                  v-model="queryParams2.tableChineseName"
                  placeholder="请输入中文名称"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="hisVersionVal" label="版本号" prop="hisVersionVal">
        <el-input disabled="true"  style="width: 300px"
                  v-model="queryParams2.hisVersionVal"
                  placeholder="请输入版本号"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="索引名称" prop="indexedName">
        <el-input
          v-model="queryParams.indexedName"
          placeholder="请输入索引名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段名称" prop="indexedColumnName">
        <el-input
          v-model="queryParams.indexedColumnName"
          placeholder="请输入字段名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序方式" prop="indexedAscDesc">
        <el-select clearable placeholder="请选择字段排序方式" v-model="queryParams.indexedAscDesc" @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in ascOrDesc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportworld:indexed:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reportworld:indexed:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reportworld:indexed:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reportworld:indexed:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border="border" v-loading="loading" max-height="550px"
              :data="indexedList" @selection-change="handleSelectionChange"
               @sort-change="handleSortChange" :row-style="tableRowStyle" @row-click="rowClick" ref="tableIndex">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-hasPermi="['reportworld:indexed:query']"
          label="操作" align="center" width="200" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleUpdate(scope.row,false)"
                v-hasPermi="['reportworld:indexed:query']"
              >详情</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="tableIndexedDemandBug(scope.row)"
                v-hasPermi="['reportworld:indexed:demandBug']"
              >修改记录</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="comparedata(scope.row)"
                v-hasPermi="['reportworld:table:indexed:comparedata']"
              >历史记录</el-button>
            </template>
        </el-table-column>
      <el-table-column label="字段名称" width="180" align="center" prop="indexedColumnName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="索引名称"  width="260" align="center" prop="indexedName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="索引字段顺序" width="230" align="center" prop="indexedColumnSequence" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="索引类型" width="180" align="center" prop="indexedType" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.table_indexed" :value="scope.row.indexedType"/>
        </template>
      </el-table-column>
      <el-table-column label="排序方式" width="180" align="center" prop="indexedAscDesc" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="索引顺序" width="200" align="center" prop="indexedSequence" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对应索引信息对话框 -->
    <el-dialog :title="title" :close-on-click-modal ="false" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="字段" prop="indexedColumnName">
              <el-select clearable style="width: 100%;" v-model="form.indexedColumnName" placeholder="请选择字段">
                <el-option
                  v-for="item in  tableColumns"
                  :key="item.columnName"
                  :label="`${item.columnName}【${item.columnChineseName}】`"
                  :value="item.columnName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="索引类型" prop="indexedType">
              <el-select clearable v-model="form.indexedType" placeholder="请选择索引类型">
                <el-option
                  v-for="dict in dict.type.table_indexed"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="索引名称" prop="indexedName">
              <el-input v-model="form.indexedName" placeholder="请输入索引名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段索引顺序" prop="indexedColumnSequence">
              <el-input-number style="width: 100%" v-model="form.indexedColumnSequence" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段排序方式" prop="indexedAscDesc">
              <el-select clearable placeholder="请选择字段排序方式" v-model="form.indexedAscDesc">
                <el-option
                  v-for="dict in ascOrDesc"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="索引顺序" prop="indexedSequence">
              <el-input-number style="width: 100%" v-model="form.indexedSequence" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_common_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input maxlength="500" v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isUpdate" :loading="butloading"  @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog v-if="openComparedata"  :title="titleComparedata" :close-on-click-modal ="false"  :visible.sync="openComparedata" width="1200px" append-to-body>
      <compare :dataId="dataId" :dataCode="dataCode"> </compare>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openComparedata = false">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog v-if="openDatademandbug"  :title="titleDatademandbug" :close-on-click-modal ="false"  :visible.sync="openDatademandbug" width="1200px" append-to-body>
      <datademandbug :dataId="dataId" :dataCode="dataCode" :hisVersion="hisVersion"> </datademandbug>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openDatademandbug = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listIndexed, getIndexed, delIndexed, addIndexed, updateIndexed,init } from "@/api/reportworld/indexed";
import { getTable} from "@/api/reportworld/table";
import { getTableRowStyle } from "@/utils/yss";
import compare from "@/views/reportworld/comparedata/index";
import datademandbug from "@/views/reportworld/demandbuginfo/datademandbug";

export default {
  name: "tableIndexed",
  components: {
    compare,
    datademandbug
  },
  dicts: ['sys_common_status','table_indexed'],
  data() {
    return {
      butloading:false,
      isUpdate:false,
      // 排序方式
      ascOrDesc: [
        {
          value: "ASC",
          label: "ASC"
        },
        {
          value: "DESC",
          label: "DESC"
        }
      ],
      // 字段信息
      tableColumns:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对应索引信息表格数据
      indexedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //历史数据
      titleComparedata:'',
      openComparedata:false,
      titleDatademandbug:'',
      openDatademandbug:false,
      dataId:'',
      dataCode:'',
      hisVersion:'',
      hisVersionVal:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        tableId: null,
        indexedColumnName: null,
        indexedName: null,
        indexedColumnSequence: null,
        indexedType: null,
        indexedAscDesc: null,
        indexedSequence: null,
        fileName: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        hisVersion:null,
      },
      queryParams2:{},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        indexedColumnName: [
          { required: true, message: "字段不能为空", trigger: "blur" }
        ],
        indexedName: [
          { required: true, message: "索引名称不能为空", trigger: "blur" }
        ],
        indexedType: [
          { required: true, message: "索引类型不能为空", trigger: "blur" }
        ],
        indexedColumnSequence: [
          { required: true, message: "字段索引顺序不能为空", trigger: "blur" }
        ],
        indexedSequence: [
          { required: true, message: "索引顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  activated() {
    const tableId = this.$route.query.tableId;
    const hisVersion = this.$route.query.hisVersion;
    if(tableId != undefined && tableId){
      this.queryParams.tableId = tableId;
      this.queryParams.hisVersion = hisVersion;
      this.hisVersionVal = false;
      if (this.queryParams.hisVersion) {
        this.hisVersionVal = true;
        this.queryParams2.hisVersionVal = this.queryParams.hisVersion;
      }
      this.init();
      this.getTableInfo();
      this.getList();
    }
  },
  created() {

  },
  methods: {
    getTableInfo(){
      const object = Object.assign({});
      object.id= this.queryParams.tableId;
      object.hisVersion= this.queryParams.hisVersion;
      getTable(JSON.stringify(object)).then(response => {
        this.queryParams2.tableName  = response.data.name;
      this.queryParams2.tableChineseName  = response.data.chineseName;
      if(this.queryParams.hisVersion){
        this.hisVersionVal=true;
        this.queryParams2.hisVersionVal=this.queryParams.hisVersion;
      }
    });
    },
    init(){
      init(this.queryParams).then(response => {
       this.tableColumns=response.tableColumns;
    });
    },
    tableIndexedDemandBug(row){
      this.openDatademandbug =true;
      this.dataId = row.id;
      this.dataCode = 'REPORT_TABLE_INDEXED_CODE';
      this.hisVersion=this.queryParams.hisVersion;
      this.titleDatademandbug = "表索引修改记录信息";
    },
    comparedata(row){
      this.openComparedata =true;
      this.dataId = row.id;
      this.dataCode = 'REPORT_TABLE_INDEXED_CODE';
      this.titleComparedata = "表索引历史记录信息";
    },
    /** 查询对应索引信息列表 */
    getList() {
      this.loading = true;
      listIndexed(this.queryParams).then(response => {
        this.indexedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tableId: null,
        indexedColumnName: null,
        indexedName: null,
        indexedColumnSequence: null,
        indexedType: null,
        indexedAscDesc: null,
        indexedSequence: null,
        fileName: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: "/reportworld/dataInfo/table" };
      this.$tab.closeOpenPage(obj);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
      /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
     },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.tableId =this.queryParams.tableId;
      this.open = true;
      this.title = "添加对应索引信息";
      this.isUpdate =true;
      this.form.indexedType = parseInt(this.dict.type.table_indexed[0].value);
      this.form.status = parseInt(this.dict.type.sys_common_status[0].value);
    },
    /** 修改按钮操作 */
    handleUpdate(row,isUpdate) {
      this.reset();
      const id = row.id || this.ids
      if(isUpdate == false){
        this.queryParams.id=id;
      }else{
        this.queryParams.id=id[0];
      }
      getIndexed(this.queryParams).then(response => {
        this.queryParams.id='';
        this.form = response.data;
        this.open = true;
        this.title = "修改对应索引信息";
        this.isUpdate =true;
      if(isUpdate == false){
        this.title = "索引信息详情";
        this.isUpdate =false;
      }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.butloading=true;
          this.form.hisVersion =this.queryParams.hisVersion;
          if (this.form.id != null) {
            updateIndexed(this.form).then(response => {
              this.open = false;
              this.$modal.msgSuccess("修改成功");
              this.getList();
            }).finally(() => {this.butloading=false});
          } else {
            addIndexed(this.form).then(response => {
              this.open = false;
              this.$modal.msgSuccess("新增成功");
              this.getList();
            }).finally(() => {this.butloading=false});
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const hisVersion= this.queryParams.hisVersion;
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        return delIndexed(ids,hisVersion);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('reportworld/indexed/export', {
        ...this.queryParams
      }, `indexed_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    rowClick(row, column, event) { //单击行
      this.$refs.tableIndex.toggleRowSelection(row);
    }
  }
};
</script>
